//login.js
//获取应用实例
var app = getApp();
Page({
  data: {
    identifyingCode:"",
    phone:"",
    idcode: "",
    server_idcode: "",
    state:{text:"已发送",type:"primary",pass:true},
    phoneNumberLength:true,
    auth_result_fail:false,
    userType:"",
    agreement:false,
    getIdentifyingCodeText:'获取验证码',
    agree:true
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    console.log(app.globalData.openid)
    wx.setNavigationBarTitle({ "title": app.appName+"--注册"});
    wx.showToast({
      title: "页面加载中\n请稍候...",
      icon: "loading",
      duration: 5000
    });
    if (e.phone) {
      this.setData({
        phone: e.phone,
        phoneNumberLength:false
      });
    }
  },
  showAgree:function(){
    this.setData({
      agree:false
    })
  },
  getPhoneNumber: function (e) {
    console.log(e)
    var that = this;
    wx.request({
      url: app.server + '/tencent/wechat/decryptText',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      data: {
        sessionKey: app.globalData.session_key,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      },
      success: function (res) {
        console.log(res);
        if(res.data && res.data.result){
          let data = JSON.parse(res.data.result)
          that.setData({
            phone:data.purePhoneNumber,
            phoneNumberLength:false
          })
        }
       
      },
      fail: function (error) {
        console.log(error);
      }
    })
  },
  rephoneauth:function(){
      wx.redirectTo({
        url: '/pages/auth/phoneauth'
      })
  },
  onReady: function(){
    wx.hideToast()
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  mobileInput:function(e){
    this.setData({
      phone:e.detail.value
    })
    if (String(e.detail.value).length > 10){
       this.setData({
         phoneNumberLength:false
       })
    }
  },
  idcodeInput:function(e){
    console.log(e.detail.value,this.data.server_idcode)
    if (e.detail.value != this.data.server_idcode){
      this.setData({
          identifyingCode: false,
          getIdentifyingCodeText: '验证码错误',
          phoneNumberLength:true
        });
        this.setData({
          state:{text:"验证码错误",type:"warn",pass:true}
        })
    }else{
      this.setData({
        identifyingCode: true,
        getIdentifyingCodeText: '验证码通过',
        phoneNumberLength:false
      });
        this.setData({
          state:{text:"验证码通过",type:"primary",pass:false}
        })
    };
  },
  checkboxChange:function(e){
    console.log(this.data.agreement)
     this.setData({
       agreement: !this.data.agreement
     })
  },
  getIdentifyingCode: function(e) {
    var that = this;
    var voice = e.currentTarget.dataset.voice == 'true'?true:false
    if (!that.data.phone){
        wx.showToast({
          title: "请输入手机号码",
          icon: "loading",
          duration: 1000
        });
        return false;
    };
    app.getIdentifyingCode(that.data.phone, voice,function(code){
      console.log(code)
      that.data.server_idcode = code;
      that.setData({
        identifyingCode: true,
        getIdentifyingCodeText: '已获取',
        phoneNumberLength:true
      });
    });
  },
  userRegister: function (e) {
    var that = this;
    wx.showLoading({
      title: '验证手机号码中...',
    });
    // that.setData({
    //   'state.pass': true
    // })
    if (!that.data.name){
      wx.hideLoading()
      wx.showModal({
        title: '温馨提示',
        content: '请输入您的真实姓名',
        success(res) {
          // that.setData({
          //   'state.pass': false
          // })
        }
      })
      return false;
    }
    if (!that.data.agreement){
      wx.hideLoading()
      wx.showModal({
        title: '温馨提示',
        content: '请阅读《软件使用协议和私隐保护政策》并勾选同意协议内容',
        success(res) {
          // that.setData({
          //   'state.pass': false
          // })
        }
      })
       return false;
    }
    wx.request({
      url: app.server +'/tencent/wechat/coid',
        method:'POST',
        header: {
              'content-type': 'application/json',
              'Cookie':app.globalData.cookie
        },
        data: {
          phone_num: that.data.phone,
          open_id:app.globalData.openid,
          name: that.data.name
        },
        success: function(res) {console.log(res);
          wx.hideLoading();
          if(res.data.success){
            wx.showToast({
              title: "绑定成功",
              icon: "success",
              duration: 2000
            });
            setTimeout(function(){
              wx.redirectTo({
                url: '/pages/index/index?id=' + res.data.id
              })
              // if (that.data.receiver) {
              //   wx.redirectTo({
              //     url: '/pages/driver/order_receiver?id=' + that.data.receiver
              //   })

              // } else {
                
              // }
            },2000)
          }else{
            wx.showToast({
              title: res.data.data,
              image: "/images/wrong.png",
              duration: 2000
            });
            setTimeout(function () {
              that.setData({
                'state.pass': true,
                idcode:'',
                getIdentifyingCodeText: '获取验证码',
                phoneNumberLength: false
              })
            }, 2000)
          }
        },
        fail:function(error){
          wx.hideLoading();
          console.log(error);
          wx.showToast({
            title: res.data.data,
            image: "/images/wrong.png",
            duration: 2000
          });
          setTimeout(function () {
            that.setData({
              'state.pass': true,
              idcode: '',
              getIdentifyingCodeText: '获取验证码',
              phoneNumberLength: false
            })
          }, 2000)
        }
      })
  }
});